<?php
//by willson 2016/5/10
//简化请求数据的获取
class Request {
	public function __construct() {
		//
	}

	public function get($name, $default='', $type='string') {
		$value = $this->_get($name, $default, $type, 'get');
		return $value;
	}

	public function post($name, $default='', $type='string') {
		$value = $this->_get($name, $default, $type, 'post');
		return $value;
	}

	public function cookie($name, $default='', $type='string') {
		$value = $this->_get($name, $default, $type, 'cookie');
		return $value;
	}

	public function server($name, $default='', $type='string') {
		$value = $this->_get($name, $default, $type, 'server');
		return $value;
	}

	public function session($name, $default='', $type='string') {
		$value = $this->_get($name, $default, $type, 'session');
		return $value;
	}

	public function _get($name, $default='', $type='string', $method='get') {
		$value = "";
		$data = array();
		switch ($method) {
			case 'get':
				$data = $_GET;
				break;
			case 'post':
				$data = $_POST;
				break;
			case 'cookie':
				$data = $_COOKIE;
				break;
			case 'server':
				$data = $_SERVER;
				break;
			case 'session':
				$data = $_SESSION;
				break;
		}
		switch ($type) {
			case 'int':
				if ($default=='') $default = 0;
				$value = isset($data[$name]) ? intval($data[$name]) : $default;
				break;
			case 'float':
				if ($default=='') $default = 0;
				$value = isset($data[$name]) ? floatval($data[$name]) : $default;
				break;
			default:
				$value = (isset($data[$name]) && $data[$name]) ? $data[$name] : $default;
				break;
		}
		return $value;
	}
}
